package com.jgzx.redis.param;

import cn.hutool.core.util.StrUtil;
import com.jgzx.redis.config.CacheKeyEnum;
import com.jgzx.redis.config.RedisEnum;
import com.jgzx.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @author jackesy
 * @className: ParamCache
 * @description: 企业参数缓存
 * @create 2021/6/16 4:11 下午
 **/
@Component
public class ParamCache {

    private static ParamCache paramCache;

    @Autowired
    private RedisService redisService;

    @PostConstruct
    public void init() {
        paramCache = this;
        paramCache.redisService = this.redisService;
    }

    /**
     * @author jackesy
     * @methodName: getParamValue
     * @description: 获取企业参数值
     * @create 2021/6/16 4:17 下午
     **/
    public static String getParamValue(Long enterprise_id, ParamKeyEnum paramKeyEnum){
        String cache_key = CacheKeyEnum.ENTERPRISE_PARAM.getValue() + enterprise_id + ":" + paramKeyEnum.getKey();
        String param_value = paramCache.redisService.get(RedisEnum.SYS_CODE_CACHE, cache_key);
        if(StrUtil.isEmpty(param_value)){
            cache_key = CacheKeyEnum.ENTERPRISE_PARAM.getValue() + "default" + ":" + paramKeyEnum.getKey();
            param_value = paramCache.redisService.get(RedisEnum.SYS_CODE_CACHE, cache_key);
        }
        return param_value;
    }
}
